//
//  TransactionViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/3/5.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "TransactionViewController.h"
#import "TranscationModel.h"
#import "LMJSettingCell.h"
#import "WebInfoViewController.h"
#import "FoundServer.h"
#import "UIButton+WebCache.h"
#define COL_COUNT 3
#define ITEM_WIDTH AdaptedWidth(50)
#define ITEM_HEIGHT AdaptedWidth(70)
@interface TransactionViewController ()
@property (nonatomic,strong) FoundServer*foundServer;
@end
@implementation TransactionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self cogfin];
}
- (void)cogfin{
    self.title = XLocalized(@"交易详情");
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, AdaptedWidth(120))];
    UILabel*quantityLab = [UILabel new];
    [headView addSubview:quantityLab];
    quantityLab.font = kBoldFont(27);
    [quantityLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    quantityLab.text = [NSString stringWithFormat:@"%@%@",[self.transcationModel.data.from isEqualToString:self.curWalletName]?@"-":@"+",self.transcationModel.data.quantity];
    self.tableView.tableHeaderView = headView;
    
    LMJWordItem *item1 = [LMJWordItem itemWithTitle:XLocalized(@"收款账号") subTitle:self.transcationModel.data.to itemOperation:^(NSIndexPath *indexPath) {
        
    }];
    item1.subTitleColor = UIColorHex(#B5B5B5);
    item1.subTitleFont = kDefaultFont(12);
    LMJWordItem *item2 = [LMJWordItem itemWithTitle:XLocalized(@"付款账号") subTitle:self.transcationModel.data.from itemOperation:^(NSIndexPath *indexPath) {
        
    }];
    item2.subTitleColor = UIColorHex(#B5B5B5);
    item2.subTitleFont = kDefaultFont(12);
    LMJWordItem *item3 = [LMJWordItem itemWithTitle:XLocalized(@"备注（Memo）") subTitle:[self.transcationModel.data.memo stringByReplacingOccurrencesOfString:@"\n" withString:@""] itemOperation:^(NSIndexPath *indexPath) {
        
    }];
    item3.subTitleColor = UIColorHex(#B5B5B5);
    item3.subTitleFont = kDefaultFont(12);
    LMJWordItem *item4 = [LMJWordItem itemWithTitle:XLocalized(@"交易号") subTitle:self.transcationModel.trx_id itemOperation:^(NSIndexPath *indexPath) {
        
    }];
    item4.subTitleColor = UIColorHex(#B5B5B5);
    item4.subTitleFont = kDefaultFont(12);
    LMJWordItem *item5 = [LMJWordItem itemWithTitle:XLocalized(@"区块号") subTitle:self.transcationModel.block_num itemOperation:^(NSIndexPath *indexPath) {
        
    }];
    item5.subTitleColor = UIColorHex(#B5B5B5);
    item5.subTitleNumberOfLines = 1;
    item5.subTitleFont = kDefaultFont(12);
    LMJWordItem *item6 = [LMJWordItem itemWithTitle:XLocalized(@"交易时间") subTitle:self.transcationModel.block_time itemOperation:^(NSIndexPath *indexPath) {
        
    }];
    item6.subTitleColor = UIColorHex(#B5B5B5);
    item6.subTitleFont = kDefaultFont(12);
    LMJWordItem *item7 = [LMJWordItem itemWithTitle:XLocalized(@"合约账号") subTitle:self.transcationModel.account itemOperation:^(NSIndexPath *indexPath) {
    }];
    item7.subTitleColor = UIColorHex(#B5B5B5);
    item7.subTitleFont = kDefaultFont(12);
    LMJItemSection *section = [LMJItemSection sectionWithItems:@[item1,item2,item3,item4,item5,item6,item7] andHeaderTitle:nil footerTitle:nil];
    [self.sections addObject:section];
    
    UIView *footV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, AdaptedWidth(300))];
    UILabel *footLab = [UILabel new];
    footLab.textColor = UIColorHex(#333333);
    footLab.font = kDefaultFont(13);
    footLab.text = XLocalized(@"-前往查询详细信息-");
    [footV addSubview:footLab];
    [footLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedWidth(8));
        make.centerX.mas_equalTo(0);
    }];
    UIView *cotentV = [UIView new];
    [footV addSubview:cotentV];
    [cotentV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(footLab.mas_bottom).offset(AdaptedWidth(8));
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    [self.foundServer getTransactionDappsCompletion:^(BOOL success, NSString * _Nonnull msg) {
        if (success) {
            kWeakSelf(self);
            for (int i = 0; i < self.foundServer.transactionList.count; i++){
                FoundRecommendModel*item = self.foundServer.transactionList[i];
                UIView *content = [UIView new];
                UIImageView*imageV = [UIImageView new];
                [content addSubview:imageV];
                [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.left.right.mas_equalTo(0);
                    make.height.equalTo(imageV.mas_width);
                }];
                UILabel*titleLabel = [UILabel new];
                titleLabel.font = kDefaultFont(13);
                titleLabel.textColor = UIColorHex(333333);
                [content addSubview:titleLabel];
                [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.bottom.left.right.mas_equalTo(0);
                    make.top.equalTo(imageV.mas_bottom);
                }];
                content.userInteractionEnabled = YES;
                [content addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
                    NSString*url = [NSString stringWithFormat:@"%@tx/%@",item.redirect_url,self.transcationModel.trx_id];
                    WebInfoViewController *info = [WebInfoViewController new];
                    info.gotoURL = url;
                    info.title = XLocalized(@"交易查询");
                    [weakself.navigationController pushViewController:info animated:YES];
                }]];
                [imageV sd_setImageWithURL:String_To_URL(item.logo)];
                titleLabel.text = item.name;
    
                [cotentV addSubview:content];
                NSInteger row = i / COL_COUNT;
                NSInteger col = i % COL_COUNT;
                CGFloat margin = (footV.width - (ITEM_WIDTH * COL_COUNT)) / (COL_COUNT + 1);
                CGFloat picX = margin + (ITEM_WIDTH + margin) * col;
                CGFloat picY = margin + (ITEM_HEIGHT + margin) * row;
                content.frame = CGRectMake(picX, picY, ITEM_WIDTH, ITEM_HEIGHT);
            }
            footV.height = (self.foundServer.transactionList.count/3+1)*2*ITEM_HEIGHT+AdaptedWidth(30);
            self.tableView.tableFooterView = footV;
        }
    }];
//    NSArray*items = @[@"EOSPark",@"EOSX",@"EOSFlare"];
//    NSArray*urls = @[@"https://eospark.com/MainNet/tx/",@"https://www.eosx.io/tx/",@"https://eosflare.io/tx/"];
//    kWeakSelf(self);
//    for (int i = 0; i < items.count; i++){
//        UIButton *btn = [[UIButton alloc] init];
//        btn.tag = i;
//        [btn addTapBlock:^(UIButton * _Nonnull selBtn) {
//            NSString*url = [NSString stringWithFormat:@"%@%@",urls[selBtn.tag],self.transcationModel.trx_id];
//            WebInfoViewController *info = [WebInfoViewController new];
//            info.gotoURL = url;
//            info.title = XLocalized(@"交易查询");
//            [weakself.navigationController pushViewController:info animated:YES];
//        }];
//        [btn setTitle:items[i] forState:UIControlStateNormal];
//        btn.titleLabel.font = kDefaultFont(13);
//        btn.titleLabel.adjustsFontSizeToFitWidth = YES;
//        [btn setTitleColor:UIColorHex(333333) forState:UIControlStateNormal];
//        [cotentV addSubview:btn];
//        NSInteger row = i / COL_COUNT;
//        NSInteger col = i % COL_COUNT;
//        CGFloat margin = (footV.width - (ITEM_WIDTH * COL_COUNT)) / (COL_COUNT + 1);
//        CGFloat picX = margin + (ITEM_WIDTH + margin) * col;
//        CGFloat picY = margin + (ITEM_HEIGHT + margin) * row;
//        btn.frame = CGRectMake(picX, picY, ITEM_WIDTH, ITEM_HEIGHT);
//    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LMJWordItem *item = self.sections[indexPath.section].items[indexPath.row];
    
    
    LMJSettingCell *cell = [LMJSettingCell cellWithTableView:tableView andCellStyle:UITableViewCellStyleSubtitle];
    
    cell.item = item;
    
    return cell;
}

#pragma mark - getter
- (FoundServer*)foundServer{
    if (!_foundServer) {
        _foundServer = [FoundServer new];
    }
    return _foundServer;
}
@end
